package net.muchine.web.sample.controller;

import net.muchine.web.sample.model.User;
import net.muchine.web.sample.model.result.Result;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sample-ajax")
public class AjaxSampleController {
	private Log log = LogFactory.getLog(this.getClass());
	
	@RequestMapping("/checkloginid/{id}")
	@ResponseBody
	public Result checkLogin(@PathVariable String id) {
		log.info("id: " + id);
		
		Result result = new Result();
		
		if ("asdf".equals(id)) {
			result.setDuplicated(true);
			result.setAvailableId("zxcv");
		}
		else {
			result.setDuplicated(false);
		}
		
		return result;
	}
	
	@RequestMapping("/new")
	public String newUser() {
		System.out.println("new user...");
		return "/sample/ajax/user";
	}
	
	@RequestMapping("/register")
	@ResponseBody
	public User createUser(@RequestBody User user) {
		if ("asd".equals(user.getLoginid())) {
			user.setLoginid("123");
		}
		
		System.out.println("user has been created... id: " + user.getLoginid());
		
		return user;
	}
}
